/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.shop.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.anny.shop.entity.AnnyShop;
import com.jeeplus.modules.anny.shop.entity.AnnyShopCategory;
import com.jeeplus.modules.anny.shop.mapper.AnnyShopCategoryMapper;
import com.jeeplus.modules.anny.shop.mapper.AnnyShopMapper;

/**
 * 用于店铺的分类Service
 * @author xy
 * @version 2018-12-03
 */
@Service
@Transactional(readOnly = true)
public class AnnyShopCategoryService extends CrudService<AnnyShopCategoryMapper, AnnyShopCategory> {
	
	@Autowired
	private AnnyShopMapper annyShopMapper;

	public AnnyShopCategory get(String id) {
		return super.get(id);
	}
	
	public List<AnnyShopCategory> findList(AnnyShopCategory annyShopCategory) {
		if (StringUtils.isBlank(annyShopCategory.getParentId())  ){
			annyShopCategory.setParentId("1");
		}
		return super.findList(annyShopCategory);
	}
	
	public Page<AnnyShopCategory> findPage(Page<AnnyShopCategory> page, AnnyShopCategory annyShopCategory) {
		if (StringUtils.isBlank(annyShopCategory.getParentId())  ){
			annyShopCategory.setParentId("1");
		}
		return super.findPage(page, annyShopCategory);
	}
	
	@Transactional(readOnly = false)
	public void save(AnnyShopCategory annyShopCategory) {
		if (StringUtils.isBlank(annyShopCategory.getParentId())  ){
			annyShopCategory.setParentId("1");
			annyShopCategory.setLevel("1");
		}else if("1" != annyShopCategory.getParentId()){
			annyShopCategory.setLevel("2");
		}
		super.save(annyShopCategory);
	}
	
	@Transactional(readOnly = false)
	public void delete(AnnyShopCategory annyShopCategory) {
		AnnyShop annyShop = new AnnyShop();
		AnnyShopCategory shopCategory = new AnnyShopCategory();
		if(annyShopCategory.getLevel() == "1" || annyShopCategory.getLevel().equals("1")){
			annyShop.setBigCategoryId(annyShopCategory.getId());
			shopCategory.setParentId(annyShopCategory.getId());
		}else if(annyShopCategory.getLevel() == "2" || annyShopCategory.getLevel().equals("2")){
			annyShop.setCategoryID(annyShopCategory.getId());
		}
		List<AnnyShop> list = annyShopMapper.findList(annyShop);
		if(list == null || list.size() == 0){
			List<AnnyShopCategory> categoryList =  this.mapper.findList(shopCategory);
			if(null != categoryList && categoryList.size() > 0){
				for (AnnyShopCategory category : categoryList) {
					super.delete(category);
				}
			}
			super.delete(annyShopCategory);
		}
	}
	
}